/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	
	/**
	 * ObjectFinder 
	 * @author puny
	 * 
	 */
	public class ObjectFinder extends Object
	{
		public function ObjectFinder(obj:Blocker)
		{
			super();
		}
		
		/**
		 * Find objects by name and return an array instance which includes them.
		 * @param container container which has display objects
		 * @param name object class name
		 * @return array instance which includes objects of the given name.
		 * 
		 */
		public static function findArray(container:DisplayObjectContainer, name:String):Array {
			var pool:Pooling = Pooling.getInstance(Array);
			var array:Array = pool.checkOut();
			while(array.length>0) {
				array.pop();
			}
			
			var i:int;
			var child:DisplayObject;
			for(i = 0; i < container.numChildren; i++) {
				child = container.getChildAt(i);
				if (child != null) {
					if (name == eliminateObjectName(child.toString())) {
						array.push(child);
					}
				} else {
					i=i+0;
				}
			}

			return array;
		}
		
		/**
		 * Find an object by name.
		 * @param container container which has display objects
		 * @param name object class name
		 * @return the first object which find by the given name.
		 * 
		 */
		public static function find(container:DisplayObjectContainer, name:String):DisplayObject {
			var i:int;
			var child:DisplayObject;
			for(i = 0; i < container.numChildren; i++) {
				child = container.getChildAt(i);
				if (child != null && name == eliminateObjectName(child.toString())) {
					return child;
				}
			}
			return null;
		}
		
		/**
		 *Eliminate an object name from toString() 
		 * @param value
		 * @return object class name
		 * 
		 */
		public static function eliminateObjectName(value:String):String {
			return value.substr(8,value.length-9);
		}
		
	}
}

internal class Blocker {
	
}